import db from '../../db/inedx.js'
import { crawlWeb } from '../../../../utils/crawler.js'
import { saveJson, sleep, random, readJson } from '../../../../utils/util.js'
import { isDev, dataDir, techniqueDir, subTechniqueDir } from '../../config/index.js'

import { glob } from 'glob'

import Page from '../../page.js'

class SubTechnique {
  constructor() {
    this.page = new Page()
  }

  get db() {
    return db.projectConfig
  }

  get config() {
    return db.projectConfig.data
  }

  // 开始任务
  async start() {
    await this.crawList()
  }

  // 抓取列表
  async crawList() {
    // var mitigations = [];
    // $('table.table-bordered tbody tr').each(function() {
    //   var row = $(this);
    //   var id = row.find('td:eq(0) a').text().trim();
    //   var name = row.find('td:eq(1) a').text().trim();
    //   var url  = 'https://attack.mitre.org'+row.find('td:eq(1) a').attr("href")
    //   var description = row.find('td:eq(2)').text().trim();
    //   mitigations.push({
    //     id: id,
    //     name: name,
    //     url,
    //     description: description
    //   });
    // });
    // console.log(mitigations);

    const mitigations = await readJson(`${dataDir}/mitigation.json`)

    for (let i = this.config.mitigationIndex; i < mitigations.length; i++) {
      const mitigation = mitigations[i]
      // const { html } = await crawlWeb(mitigation.url)
      await this.crawlContent(mitigation.url, mitigation)
    }
  }

  // 抓取内容
  async crawlContent(url, item = {}) {
    await sleep(random(5000, 10000)) // 延迟1-5秒执行
    const { $ } = await crawlWeb(url)
    const json = this.getContentJson($)
    const data = Object.assign(item, json)

    saveJson(`${dataDir}/mitigation/${data.id}.json`, data)

    // this.config.subTechniqueContentIndex += 1
    // await this.db.write()
  }

  getContentJson($) {
    // Extract main title
    var title = $('h1').text().trim()

    // Extract description
    var content = $('.description-body').html().trim()

    // Extract metadata from card
    var metadata = {}
    $('.card-body .card-data').each(function () {
      var key = $(this).find('.h5').text().trim().replace(':', '')
      var value = $(this).contents().last().text().trim()
      metadata[key] = value
    })

    // Extract techniques
    var techniques = []
    let techniqueId = ''
    let domain = ''
    $('.techniques-used tbody tr').each(function () {
      // Skip sub-technique rows (they have empty first cell)
      const tdCount = $(this).find('td').length

      let subTechniqueId = ''

      techniqueId = $(this).find('td:eq(1) a').text() || techniqueId
      domain = $(this).find('td:eq(0)').text().trim() || domain

      if (tdCount === 5) {
        subTechniqueId = $(this).find('td:eq(2) a').text()
      }

      var technique = {
        domain,
        // id: $(this).find('td:eq(1) a').attr('href') || $(this).find('td:eq(2) a').attr('href'),
        id: techniqueId + subTechniqueId,
        name: $(this).find('td:eq(3)').text().trim(),
        use: $(this).find('td:eq(4)').text().trim(),
      }
      techniques.push(technique)
    })

    // Extract references
    var references = []
    $('.scite-citation-text a').each(function () {
      references.push({
        id: $(this).attr('name'),
        text: $(this).text().trim(),
        url: $(this).attr('href'),
      })
    })

    // Combine all extracted data
    var extractedData = {
      title,
      content,
      metadata,
      techniques,
      references,
    }

    // console.log(extractedData)

    return extractedData
  }
}

new SubTechnique().start()
// new SubTechnique().crawlContent('https://attack.mitre.org/mitigations/M1049')

export default SubTechnique
